package major

import (
	"admissionsystem/models"
	"admissionsystem/pkg/app"
	"admissionsystem/pkg/e"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func GetMajordictionary(c *gin.Context) {
	page := -1
	if arg := c.Query("page"); arg != "" {
		page = com.StrTo(arg).MustInt()
	}
	limit := -1
	if arg := c.Query("limit"); arg != "" {
		limit = com.StrTo(arg).MustInt()
	}
	searchText := ""
	if arg := c.PostForm("searchText"); arg != "" {
		searchText = arg
	}
	majorParam := map[string]interface{}{
		"page":       page,
		"limit":      limit,
		"searchText": searchText,
	}
	err, info, total := models.GetAllMajordictionary(majorParam)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "查询成功")
}

func AddMajordictionary(c *gin.Context){
	var majordata models.Majordictionary
	c.ShouldBind(&majordata)
	err, info := models.AddMajordictionary(majordata)
	if err != nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
		app.OK(c,info,"插入成功")	
}

func UpadateMajordictionary(c *gin.Context){
	var majordata models.Majordictionary
	c.ShouldBind(&majordata)
	err, info := models.UpdateMajordictionary(majordata.Mno,majordata)
	if err != nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
		app.OK(c,info,"修改成功")	
}

func DeleteMajordictionary(c *gin.Context){
	var majordata models.Majordictionary
	c.ShouldBind(&majordata)
	err, info := models.DeleteMajordictionary(majordata.Mno)
	if err != nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
		app.OK(c,info,"删除成功")	
}
